package com.qzyh.esb.egsa.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qzyh.esb.egsa.bean.vo.RequirementContentVO;
import com.qzyh.esb.egsa.bean.vo.UserRoleAndDeptVO;
import com.qzyh.esb.egsa.entity.Requirement;
import com.qzyh.esb.egsa.entity.RequirementContent;
import com.qzyh.esb.egsa.service.IRequirementContentService;
import com.qzyh.esb.egsa.service.IRequirementService;
import com.qzyh.esb.egsa.utils.FillUtil;
import com.qzyh.esb.egsa.utils.PageResultObject;
import com.qzyh.esb.egsa.utils.ResultObject;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 需求内容表 前端控制器
 * </p>
 *
 * @author whq
 * @since 2023-09-19
 */
@RestController
@RequestMapping("/requirementContent")
public class RequirementContentController {

    private static final Logger logger = LoggerFactory.getLogger(RequirementController.class);

    @Autowired
    private IRequirementContentService requirementContentService;

    @Autowired
    private IRequirementService requirementService;

    @GetMapping("/list")
    @ApiOperation(value = "查询需求信息列表")
    public PageResultObject<RequirementContentVO> list(@ApiParam(value = "页码") @RequestParam("pageNum") Long pageNum,
                                                       @ApiParam(value = "每页条数") @RequestParam("pageSize") Long pageSize,
                                                       @ApiParam(value = "需求名称") @RequestParam("requirementName") String requirementName,
                                                       @ApiParam(value = "需求号") @RequestParam("requirementNum") String requirementNum,
                                                       @ApiParam(value = "服务场景码") @RequestParam("serviceScence") String serviceScence,
                                                       @ApiParam(value = "起始日期") @RequestParam("startDate") String startDate,
                                                       @ApiParam(value = "截止日期") @RequestParam("endDate") String endDate,
                                                       @ApiParam(value = "状态") @RequestParam("status") String status) {
        IPage<RequirementContentVO> page = new Page<>(pageNum == null ? 0 : pageNum, pageSize == null ? 10 : pageSize);
        page = requirementContentService.getList(page, requirementName, requirementNum, serviceScence, startDate, endDate, status);
        PageResultObject<RequirementContentVO> pageResultObject = PageResultObject.success();
        pageResultObject.setPageNum(page.getCurrent());
        pageResultObject.setPageSize(page.getSize());
        pageResultObject.setTotal(page.getTotal());
        pageResultObject.setData(page.getRecords());
        return pageResultObject;
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存需求信息")
    public ResultObject save(@RequestBody RequirementContent requirementContent) {
        logger.info("请求参数：" + requirementContent.toString());
        return requirementContentService.saveOrUpdateRequirementInfo(requirementContent);
    }

    @GetMapping("/delete")
    @ApiOperation(value = "根据id删除需求信息")
    public ResultObject deleteById(@ApiParam(value = "需求id") @RequestParam("id") String id) {
        logger.info("删除需求信息id：" + id);
        boolean flag = requirementContentService.removeById(id);
        if (flag) return ResultObject.success("删除成功");
        else return ResultObject.fail("删除失败");
    }

    @GetMapping("/getRequirementList")
    @ApiOperation(value = "根据关键字查询需求信息")
    public ResultObject<List<Requirement>> getRequirementList(@ApiParam(value = "查询关键字") @RequestParam("keywords") String keywords) {
        logger.info("查询关键字："+keywords);
        return requirementContentService.getRequirementList(keywords);
    }

    @GetMapping("/changeStatus")
    @ApiOperation(value = "修改开发状态")
    public ResultObject changeStatus(@ApiParam(value = "需求内容id") @RequestParam("id") String id,@ApiParam(value = "类型") @RequestParam("type") String type) {
        logger.info("请求参数：需求内容id："+id);
        logger.info("请求参数：类型："+type);
        return requirementContentService.changeStatus(id,type);
    }

}
